package com.caxxb.dao.impl;

import com.caxxb.dao.UserDao;
import com.caxxb.entity.User;
import com.caxxb.util.DBUtil;
import com.caxxb.util.PageData;

import java.util.List;

public class UserDaoImpl implements UserDao {

    //登录的方法
    @Override
    public List<User> login(String account_number, String password) {
        String sql="SELECT * from user where account_number = ? and password = ?";
        List<User> list = DBUtil.exQuery(sql, User.class,account_number,password);
        return list;
    }

    //通过id获取user
    @Override
    public List<User> getUserById(int id) {
        String sql="SELECT * from user where user_id = ?";
        List<User> list = DBUtil.exQuery(sql, User.class, id);
        return list;
    }
    //增加用户的方法
    @Override
    public boolean addUser(User user) {
        String sql ="insert into user values (null,?,?,?,?,?,1)";
        int result = DBUtil.exUpdate(sql,user.getUser_name(),user.getSex(),user.getAccount_number(),user.getPassword(),user.getEmail());
        return result>0;
    }

    /**
     * 逻辑删除用户
     * @param user_id
     * @return
     */
    @Override
    public boolean delUser(int user_id) {
        String sql = "update user set status = 0 where user_id = ? and status = 1";
        int result = DBUtil.exUpdate(sql,user_id);
        return result>0;
    }

    @Override
    public boolean updateUser(int user_id, User user) {
        String sql ="UPDATE `user` SET `user_name`=?, `sex`=?, `account_number`=?, `password`=?, `email`=? WHERE (`user_id`=?)";
        int result = DBUtil.exUpdate(sql,user.getUser_name(),user.getSex(),user.getAccount_number(),user.getPassword(),user.getEmail(),user_id);
        return result>0;
    }

    @Override
    public PageData<User> getUserByPage(String keywords, int pageNo, int pageSize) {
        String sql = "SELECT user .* from user where user_name like ?";
        PageData<User> pageData = DBUtil.exQueryByPage(sql, User.class, pageNo,pageSize,"%"+keywords+"%");
        return pageData;
    }

    @Override
    public PageData<User> getNumByPage(String num, int pageNo, int pageSize) {
        String sql = "SELECT user .* from user where account_number like ? and status = 1";
        PageData<User> pageData = DBUtil.exQueryByPage(sql, User.class, pageNo,pageSize,num);
        return pageData;
    }

    @Override
    public List<User> getusers() {
        String sql = "select * from user";
        List list = DBUtil.exQuery(sql, User.class);
        return list;
    }

    @Override
    public boolean UpdateUserEmail(String email,int id) {
        String sql="update user set email = ? where user_id =?  ";
        int result = DBUtil.exUpdate(sql, email, id);
        return result >0;
    }

    @Override
    public boolean UpdateUserPwd(String password, int id) {
        String sql="update user set password = ? where user_id =?  ";
        int result = DBUtil.exUpdate(sql, password, id);
        return result >0;
    }

}
